﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using System.Data.SqlClient;
namespace _201206QuanLyBanHang.BaoCao
{
    public partial class FormBaoCaoDoanhThu_KhuVuc_KhachHang : Form
    {
        BLL_BaoCao bc = new BLL_BaoCao();
        AutoCompleteStringCollection auto2 = new AutoCompleteStringCollection();
        string sql1 = "select tencuahang from khachhang";
        string sql2 = "select tenkhuvuc as tencuahang from khuvuc";

        private void LoadDataToCollection(string sql)
        {
            DataConn da = new DataConn();
            AutoCompleteStringCollection auto2 = new AutoCompleteStringCollection();
            SqlDataReader reader;
            auto2.Clear();
            reader = da.ThucHienReader(sql);
            if (reader != null)
            {
                while (reader.Read())
                {
                    auto2.Add(reader["tencuahang"].ToString());
                }
            }
            textBox_TimKiembc.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            textBox_TimKiembc.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBox_TimKiembc.AutoCompleteCustomSource = auto2;
        }
     
        public FormBaoCaoDoanhThu_KhuVuc_KhachHang()
        {
            InitializeComponent();
            LoadDataToCollection(sql1);
        }

        private void FormBaoCaoDoanhThu_KhuVuc_KhachHang_Load(object sender, EventArgs e)
        {

            this.reportViewerKetQuaBC.RefreshReport();
            this.reportViewerLoLai.RefreshReport();
        }

        private void radioButtonKhuVuc_CheckedChanged(object sender, EventArgs e)
        {
            LoadDataToCollection(sql2);
            textBox_TimKiembc.Text = "";
            //groupPanel_report.Visible = false;
        }

        private void radioButtonKhachHang_CheckedChanged(object sender, EventArgs e)
        {
            LoadDataToCollection(sql1);
            textBox_TimKiembc.Text = "";
            //groupPanel_report.Visible = false;
        }

        //Load dữ liệu vào report 
        private void loaddulieuvaoreport(string strbien)
        {
            DataTable table = new DataTable();
            table.Clear();            
            if (radioButtonKhachHang.Checked == true)
            {
                //Báo cáo "doanh thu" theo khách hàng 
                if (radioButtonDoanhThu.Checked == true)
                {
                    groupPanelDoanhThu.Visible = true;
                    groupPanelLoLai.Visible = false;
                    table = editDoanhThuKhachHang(strbien);
                    reportViewerKetQuaBC.Reset();
                    //gán tên report cần hiển thị trong khung nhìn viewer
                    reportViewerKetQuaBC.LocalReport.ReportPath = @"..\\..\\BaoCao\\rptBaoCaoDoanhThuTheoQuan.rdlc";
                    //làm sạch khung nhìn
                    reportViewerKetQuaBC.LocalReport.DataSources.Clear();
                    reportViewerKetQuaBC.LocalReport.SetParameters(new ReportParameter("ReportParameterTenQuan", textBox_TimKiembc.Text));

                    reportViewerKetQuaBC.LocalReport.SetParameters(new ReportParameter("ReportParameterNoCu", bc.getCongNo("").ToString()));
                    reportViewerKetQuaBC.LocalReport.SetParameters(new ReportParameter("ReportParameterNoMoi", bc.getCongNoMoi("").ToString()));
                    ReportDataSource newDataSource = new ReportDataSource("DataSetBCDoanhThuTheoQuan", table);
                    //ReportDataSource newdatasource = new ReportDataSource("DataSetBaoCaoDoanhThuKhuVuc1", table);
                    reportViewerKetQuaBC.LocalReport.DataSources.Add(newDataSource);
                    //lam tuoi report
                    reportViewerKetQuaBC.RefreshReport();
                }
                //Báo cáo "Lỗ Lãi" theo khách hàng
                if (radioButtonLoLai.Checked == true)
                {
                    groupPanelDoanhThu.Visible = false;
                    groupPanelLoLai.Visible = true;
                    MessageBox.Show("Lỗ lãi khách hàng");
                }
               
            }
            if (radioButtonKhuVuc.Checked == true)
            {
                //Báo cáo "doanh thu" theo khu vực
                if (radioButtonDoanhThu.Checked == true)
                {
                    groupPanelDoanhThu.Visible = true;
                    groupPanelLoLai.Visible = false;
                    table = editDoanhThuKhuVuc(strbien);
                    //reset lai khung hiển thị report
                    reportViewerKetQuaBC.Reset();
                    //gán tên report cần hiển thị trong khung nhìn viewer
                    //reportViewerKetQuaBC.LocalReport.ReportPath = @"D:\Project\Soft\Code2013\201206QuanLyBanHang\BaoCao\rptBaoCaoDoanhThuKhuVuc.rdlc";
                    reportViewerKetQuaBC.LocalReport.ReportPath = @"..\\..\\BaoCao\\rptBaoCaoDoanhThuKhuVuc.rdlc";
               
                    //reportViewerKetQuaBC.LocalReport.ReportPath = @"..//BaoCao//rptBaoCaoDoanhThuKhuVuc.rdlc";
                    //reportViewerKetQuaBC.LocalReport.ReportPath = @"\BaoCao\rptBaoCaoDoanhThuKhuVuc.rdlc";
                    //làm sạch khung nhìn
                    reportViewerKetQuaBC.LocalReport.DataSources.Clear();
                    reportViewerKetQuaBC.LocalReport.SetParameters(new ReportParameter("ReportParameterKhuVuc", textBox_TimKiembc.Text));
                    ReportDataSource newDataSource = new ReportDataSource("DataSetBCDoanhThuKhuVuc", table);
                    //ReportDataSource newdatasource = new ReportDataSource("DataSetBaoCaoDoanhThuKhuVuc1", table);
                    reportViewerKetQuaBC.LocalReport.DataSources.Add(newDataSource);
                    //lam tuoi report
                    reportViewerKetQuaBC.RefreshReport();
                }
                //Báo cáo "Lỗ Lãi" theo Khu vực
                if (radioButtonLoLai.Checked == true)
                {
                    groupPanelDoanhThu.Visible = false;
                    groupPanelLoLai.Visible = true;
                    //MessageBox.Show("Lỗ lãi Khu vực");
                    //table = editDoanhThuKhuVuc(strbien);
                    table = editLoLaiKhuVuc("0");
                    //reset lai khung hiển thị report
                    reportViewerLoLai.Reset();
                    //gán tên report cần hiển thị trong khung nhìn viewer
                    reportViewerLoLai.LocalReport.ReportPath = @"..\\..\\BaoCao\\rptBaoCaoLoLaiKhachHang.rdlc";

                    //reportViewerKetQuaBC.LocalReport.ReportPath = @"..//BaoCao//rptBaoCaoDoanhThuKhuVuc.rdlc";
                    //reportViewerKetQuaBC.LocalReport.ReportPath = @"\BaoCao\rptBaoCaoDoanhThuKhuVuc.rdlc";
                    //làm sạch khung nhìn
                    reportViewerLoLai.LocalReport.DataSources.Clear();
                   // reportViewerLoLai.LocalReport.SetParameters(new ReportParameter("ReportParameterKhuVuc", textBox_TimKiembc.Text));
                    ReportDataSource newDataSource = new ReportDataSource("DataSetBCLoLaiKhuVuc", table);
                    //ReportDataSource newdatasource = new ReportDataSource("DataSetBaoCaoDoanhThuKhuVuc1", table);
                    reportViewerLoLai.LocalReport.DataSources.Add(newDataSource);
                    //lam tuoi report
                    reportViewerLoLai.RefreshReport();
                }
                               
            }

        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (textBox_TimKiembc.Text == "")
            {
                MessageBox.Show("Chọn khách hàng hoặc khu vực cần xem báo cáo!!!");
                textBox_TimKiembc.Focus();
            }
            else
            {
                loaddulieuvaoreport(textBox_TimKiembc.Text);                 
            }
            

        }

        #region Code thay đổi giá trị cùng khách hàng, ngày mua nhưng thanh toán CHUNG

        //Chỉnh sửa  doanh thu khu vực
        public DataTable editDoanhThuKhuVuc(string khuvuc)
        {
            DataTable dt = new DataTable();
            dataGridView_temp.DataSource = bc.getDoanhThuKhuVuc(khuvuc);
            //dt = bc.getDSKhachHangKV(comboBox_KhuVuc.Text);
            //int don = dt.Rows.Count;
            for (int i = 1; i < dataGridView_temp.RowCount; i++)
            {
                //Lấy giá trị "stt" tại cell thứ i-1 so sánh với cell thứ i 
                DataGridViewCell cellsttn = dataGridView_temp.Rows[i - 1].Cells["stt"];
                String strn = cellsttn.FormattedValue.ToString();
                DataGridViewCell cellstti = dataGridView_temp.Rows[i].Cells["stt"];
                String stri = cellstti.FormattedValue.ToString();
                if (strn == stri)
                {
                    dataGridView_temp.Rows[i].Cells["thanhtoan"].Value = 0;
                    dataGridView_temp.Rows[i].Cells["congno"].Value = 0;
                }
            }
            dt = dataGridView_temp.DataSource as DataTable;
            return dt;
        }
        //Chỉnh sửa  doanh thu khách hàng
        public DataTable editDoanhThuKhachHang(string khachhang)
        {
            DataTable dt = new DataTable();
            dataGridView_temp.DataSource = bc.getDoanhThuKhachHang(khachhang);
            //dt = bc.getDSKhachHangKV(comboBox_KhuVuc.Text);
            //int don = dt.Rows.Count;
            for (int i = 1; i < dataGridView_temp.RowCount; i++)
            {
                //Lấy giá trị "stt" tại cell thứ i-1 so sánh với cell thứ i 
                DataGridViewCell cellsttn = dataGridView_temp.Rows[i - 1].Cells["ngaymua"];
                String strn = cellsttn.FormattedValue.ToString();
                DataGridViewCell cellstti = dataGridView_temp.Rows[i].Cells["ngaymua"];
                String stri = cellstti.FormattedValue.ToString();
                if (strn == stri)
                {
                    dataGridView_temp.Rows[i].Cells["sotienthanhtoan"].Value = 0;
                    //dataGridView_temp.Rows[i].Cells["congno"].Value = 0;
                }
            }
            dt = dataGridView_temp.DataSource as DataTable;
            return dt;
        }

        #endregion


        #region Code Lỗ Lãi
        //Chỉnh sửa  Lỗ lãi khu vực
        public DataTable editLoLaiKhuVuc(string khuvuc)
        {
            DataTable dt = new DataTable();
            dataGridView_temp.DataSource = bc.getDSLoLaiKhuVuc(khuvuc);
            //dt = bc.getDSKhachHangKV(comboBox_KhuVuc.Text);
            //int don = dt.Rows.Count;
            for (int i = 1; i < dataGridView_temp.RowCount; i++)
            {
                //Lấy giá trị "stt" tại cell thứ i-1 so sánh với cell thứ i 
                DataGridViewCell cellsttn = dataGridView_temp.Rows[i - 1].Cells["stt"];
                String strn = cellsttn.FormattedValue.ToString();
                DataGridViewCell cellstti = dataGridView_temp.Rows[i].Cells["stt"];
                String stri = cellstti.FormattedValue.ToString();
                if (strn == stri)
                {
                    dataGridView_temp.Rows[i].Cells["thanhtoan"].Value = 0;
                    dataGridView_temp.Rows[i].Cells["dautu"].Value = 1000;
                    dataGridView_temp.Rows[i].Cells["khuyenmai"].Value = 1000;
                    dataGridView_temp.Rows[i].Cells["loinhuan"].Value = 1000;
                }
            }
            dt = dataGridView_temp.DataSource as DataTable;
            return dt;
        }
        #endregion
        public ReportDataSource newdatasource { get; set; }

        private void comboBoxEx1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
